'''
Created on Apr 28, 2011

@author: yc30v1m
'''
from library.AutoLoad import AutoLoad
from library.Utils import singleton
import logging
log = logging.getLogger(__name__)


@singleton
class Automation(object):
    '''
    classdocs
    '''
    aConfigs = ""
    log = ""
    polling = ""
    
    def __init__(self):
        try:
            
            self.polling = AutoLoad.ClassAsObject('plugins.FolderPolling')
            self.initializeAutomation()
        except:
            log.warning("Exception Occured")

        
    def initializeAutomation(self):
        """
        This method used for initialize the automation class
        """
        #polling = AutoLoad.ClassAsObject('plugins.FolderPolling')
        
        if self.polling.getPollingStatus():
            if self.polling.isTriggerFileEnabled():
                if self.polling.isTriggerFileAvailable():
                    self.polling.archiveTriggerFile()
                    self.pollingDecision()
                else:
                    print "Trigger file notavailable"
                    pass
            else:
                self.pollingDecision()
        else:
            self.loadTestCasesForExecution(False,True)
    
    def pollingDecision(self):
        log.debug('Polling Enabled Scanning Directory now')
        self.loadTestCasesForExecution(self.polling.getIncommingFileNamesAssoc())
        self.polling.archiveAllFiles()
            
    def loadTestCasesForExecution(self,incommingFiles=False, nonPolling=False):
        AggregatorObj = AutoLoad.ClassAsObject('library.testcases.Aggregator')
        tcases = False
        if incommingFiles:
            log.debug('Execution in Polling Mode & Files are available')
            tcases = AggregatorObj.getSpecificTestCasesByFileNames(incommingFiles)
        if nonPolling:
            log.debug('Test Execution in NON POLLING MODE')
            tcases = AggregatorObj.getAllTestCases()
        if tcases:
            testCaseFactory = AutoLoad.ClassOnly('library.executors.Factory')
            testCaseFactory(tcases)    
    
        
    
